package cn.szabadsag.user.service.impl;

import cn.szabadsag.base.exception.BusinessException;
import cn.szabadsag.base.service.impl.BaseServiceImpl;
import cn.szabadsag.user.domain.LoginInfo;
import cn.szabadsag.user.domain.User;
import cn.szabadsag.user.domain.dto.UserDto;
import cn.szabadsag.user.mapper.LoginInfoMapper;
import cn.szabadsag.user.mapper.UserMapper;
import cn.szabadsag.user.service.IUserService;
import cn.szabadsag.utils.MD5Utils;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @author yangcong
 * @date 2022年03月10日 19:23
 * @since
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    /**
     * 根据手机号查询对象
     * @param phone
     * @return
     */
    @Override
    public User loadByPhone(String phone) {
        return userMapper.loadByPhone(phone);
    }

    /**
     * 注册验证
     * @param userDto
     */
    @Override
    public void register(UserDto userDto) throws BusinessException {

        //  1.验证数据
        verifyData(userDto);

        //  2.将UserDto转化成User对象
        User user = userDto2User(userDto);

        //  3.将User对象转化成LoginInfo对象
        LoginInfo loginInfo = user2LoginInfo(user);

        //  4.存储对象
        //  先存储loginIfo,然后将loginInfo_id存储在user中，最后存储user对象
        loginInfoMapper.save(loginInfo);

        user.setLogininfo_id(loginInfo.getId());

        userMapper.save(user);
    }

    /**
     * 将User对象转化成LoginInfo对象
     * @param user
     * @return
     */
    private LoginInfo user2LoginInfo(User user) {

        LoginInfo loginInfo = new LoginInfo();

        //  属性拷贝
        BeanUtils.copyProperties(user,loginInfo);

        //  1表示前端用户，0表示后端管理员账户
        loginInfo.setType(1);
        //  激活状态
        loginInfo.setDisable(user.getState());

        return loginInfo;
    }

    /**
     * 将UserDto转化成User对象
     * @param userDto
     * @return
     */
    private User userDto2User(UserDto userDto) {

        User user = new User();

        user.setUsername("p_" + userDto.getPhone());
        user.setPhone(userDto.getPhone());

        String salt = UUID.randomUUID().toString();
        //  MD5加密
        String password = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setPassword(password);
        user.setSalt(salt);
        /**
         * 1表示启用/激活
         */
        user.setState(1);
        user.setCreatetime(new Date());

        //user.setLogininfo_id();

        return user;
    }

    /**
     * 验证数据
     * @param userDto
     */
    private void verifyData(UserDto userDto) throws BusinessException {

        // 手机号不能为空
        if (StringUtils.isNullOrEmpty(userDto.getPhone())) {
            throw new BusinessException("手机号不能为空");
        }
        // 验证码不能为空
        if (StringUtils.isNullOrEmpty(userDto.getCode())) {
            throw new BusinessException("验证码不能为空");
        }
        // 密码不能为空
        if (StringUtils.isNullOrEmpty(userDto.getPassword())) {
            throw new BusinessException("密码不能为空");
        }

        //  判断验证码是否正确
        String codeInRedis = (String)redisTemplate.opsForValue().get(userDto.getPhone() + ":" + userDto.getCodeType());
        if (codeInRedis == null) {
            throw new BusinessException("验证码无效");
        }
        if (!codeInRedis.split(":")[0].equals(userDto.getCode())) {
            throw new BusinessException("验证码错误");
        }

    }
}
